package com.lpf.back.controller;

import com.lpf.back.common.CommonRes;
import com.lpf.back.domain.Activity;
import com.lpf.back.domain.Admin;
import com.lpf.back.domain.User;
import com.lpf.back.domain.vo.ActivityUserVO;
import com.lpf.back.domain.vo.UserDynamicVO;
import com.lpf.back.mapper.ActivityMapper;
import com.lpf.back.mapper.UserActivityMapper;
import com.lpf.back.service.interfaces.ActivityService;
import com.lpf.back.service.interfaces.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.In;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/activity")
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    @Autowired
    private AdminService adminService;


    @RequestMapping(value = "/gotoRelease")
    public String gotoRelease(){
        return "releaseActivity";
    }

    @RequestMapping("/releaseActivity")
    public String releaseActivity(HttpServletRequest request, String title, String description, HttpSession session, Model model){
        String status = request.getParameter("status");
        int activityStatus = -1;
        if("敬请期待".equals(status)){
            activityStatus = 2;
        } else if ("进行中".equals(status)){
            activityStatus = 1;
        } else {
            activityStatus = 0;
        }
        String username = (String) session.getAttribute("username");
        Admin admin = adminService.selectAdmin(username);
        Activity activity = new Activity();
        activity.setId(null);
        activity.setTitle(title);
        activity.setLikes(0);
        activity.setAdminid(admin.getId());
        activity.setReleaseTime(new Date(System.currentTimeMillis()));
        activity.setActiveStatus(activityStatus);
        activity.setParticipateNum(0);
        activity.setDescription(description);
        int res = activityService.releaseActivity(activity);
        model.addAttribute("发布成功！", res);
        return "redirect:/admin/activityManagement";
    }

    @RequestMapping("/editActivity")
    public String editActivity(Integer id, HttpSession session){
        Activity activity = activityService.getActivity(id);
        session.setAttribute("activity", activity);
        return "activityEdit";
    }

    @RequestMapping("/updateActivity")
    public String updateActivity(HttpServletRequest request, String title, String description, HttpSession session, Model model){
        int res = activityService.updateActivity(request, title, description, session);
        model.addAttribute("更新成功！", res);
        return "redirect:/admin/activityManagement";
    }

    @RequestMapping(value = "/getActivity", method = RequestMethod.GET)
    @ResponseBody
    public CommonRes getActivity(){
        List<Activity> activities = activityService.getAllActivity();
        return new CommonRes(activities, 1, "获取活动列表成功！");
    }

//    @RequestMapping(value = "/getActivities", method = RequestMethod.GET)
//    @ResponseBody
//    public CommonRes getAc

    @ResponseBody
    @RequestMapping("/participateActivity")
    public CommonRes participateActivity(Integer userid, Integer activityid){
        return activityService.participateActivity(userid, activityid);
    }

    @ResponseBody
    @RequestMapping("/likeActivity")
    public CommonRes likeActivity(Integer userid, Integer activityid){
        Activity activity = activityService.likeActivity(userid, activityid);
        List<Activity> activities = activityService.getAllActivity();
        if (activity == null){
            return new CommonRes(null, 0, "无法重复点赞");
        } else {
            return new CommonRes(activities, 1, "点赞成功");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/userList", method = RequestMethod.GET)
    public CommonRes userList(Integer activityid){
        List<UserDynamicVO> users = activityService.getUserList(activityid);
        if (users != null){
            return new CommonRes(users, 1, "参与用户返回成功！");
        } else {
            return new CommonRes(users, 0, "没有参与用户");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/getMyActivity", method = RequestMethod.GET)
    public CommonRes getMyActivity(Integer userid){
        List<ActivityUserVO> myActivity =  activityService.getMyActivity(userid);
        if(myActivity != null){
            return new CommonRes(myActivity, 1, "我的活动返回成功！");
        } else {
            return new CommonRes(null, 0, "我还没有参加过任何活动！");
        }
    }
}
